package yxy.game.pm2.bean;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.db.newDao.PrivilegeDAO;
import yxy.game.pm2.bean.db.po.PrivilegePo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 特权
 */
public class Privilege {

    // bean ----------------------------------------------
    private int privilegeid;
    private DateTime endtime;

    public int getPrivilegeid() {
        return privilegeid;
    }

    public void setPrivilegeid(int privilegeid) {
        this.privilegeid = privilegeid;
    }

    public DateTime getEndtime() {
        return endtime;
    }

    public void setEndtime(DateTime endtime) {
        this.endtime = endtime;
    }

    // func ----------------------------------------------

    static public boolean add(String playerCode, int privilegeid, DateTime endtime) {
        Privilege p = new Privilege();
        p.setPrivilegeid(privilegeid);
        p.setEndtime(endtime);
        return set(playerCode, p);
    }

    static public void remove(String playerCode, int privilegeid) {
        try {
            RMap<Integer, Privilege> pds = touch(playerCode);
            pds.remove(privilegeid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // redis ---------------------------------------------
    static public String key(String playerCode) {
        return String.format("PLAYER:%s:PRIVILEGE", playerCode);
    }

    static private RedisConnection redis() {
        return RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
    }

    static private RMap<Integer, Privilege> touch(String playerCode) throws RedisException {
        return RedisManager.client(redis()).getMap(key(playerCode));
    }

    static public int size(String playerCode) {
        try {
            return touch(playerCode).size();
        } catch (RedisException e) {
        }
        return 0;
    }

    static public Map<Integer, Privilege> all(String playerCode) {
        try {
            Map<Integer, Privilege> ps = touch(playerCode).readAllMap();
            if (ps.isEmpty()) {
                List<PrivilegePo> privilegePoList = PrivilegeDAO.getList(playerCode);
                if (privilegePoList != null && privilegePoList.size() > 0) {
                    for (int i = 0; i < privilegePoList.size(); i++) {
                        Privilege privilege = new Privilege();
                        privilege.setPrivilegeid(privilegePoList.get(i).getType());
                        if (privilegePoList.get(i).getEndTime() != null) {
                            privilege.setEndtime(new DateTime(privilegePoList.get(i).getEndTime()));
                        }
                        touch(playerCode).put(privilegePoList.get(i).getType(), privilege);
                    }
                }
            }
            return ps;
        } catch (RedisException e) {
        }
        return new HashMap<>();
    }

    static public Privilege get(String playerCode, int privilegeid) {
        try {
            Map<Integer, Privilege> all = all(playerCode);
            Privilege module = all.get(privilegeid);

            return module;
        } catch (Exception e) {
            return null;
        }
    }


    static public boolean set(String playerCode, Privilege p) {
        try {
            RMap<Integer, Privilege> pds = touch(playerCode);
            pds.put(p.getPrivilegeid(), p);
            return true;
        } catch (RedisException e) {
            e.printStackTrace();
            return false;
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            if (touch(playerCode).isExists()) {
                long updateTime = System.currentTimeMillis();
                List<PrivilegePo> privilegePoList = new ArrayList<>();
                Map<Integer, Privilege> ps = touch(playerCode).readAllMap();
                for (int key : ps.keySet()) {
                    Privilege privilege = ps.get(key);
                    PrivilegePo privilegePo = new PrivilegePo();
                    privilegePo.setPlayerCode(playerCode);
                    privilegePo.setType(key);
                    if (privilege.getEndtime() != null) {
                        privilegePo.setEndTime(privilege.getEndtime().toDate());
                    }
                    privilegePo.setUpdateTime(updateTime);
                    privilegePoList.add(privilegePo);
                }
                PrivilegeDAO.replace(privilegePoList);
                PrivilegeDAO.delete(playerCode, updateTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            RedisManager.client(redis()).getBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }


    //将po转成bean
    private static Privilege toBean(PrivilegePo po) {
        Privilege privilege = new Privilege();
        privilege.setEndtime(new DateTime(po.getEndTime()));
        privilege.setPrivilegeid(po.getType());
        return privilege;
    }

    @Override
    public String toString() {
        return "Privilege{" +
                "privilegeid=" + privilegeid +
                ", endtime=" + endtime +
                '}';
    }
}
